package com.sf.hackathons.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sf.hackathons.app.R;
import com.sf.hackathons.app.bean.MailInfoCommitBean;
import com.sf.hackathons.app.presenter.constract.MailInfoConstract;
import com.sf.hackathons.app.presenter.impl.MailInfoPresenter;
import com.sf.hackathons.app.utils.AddressPickTask;
import com.sf.hackathons.lib.commui.base.BaseActivity;
import com.sf.hackathons.lib.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.entity.City;
import cn.addapp.pickers.entity.County;
import cn.addapp.pickers.entity.Province;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.SinglePicker;

/**
 * 寄件信息
 */
public class MailInfoActivity extends BaseActivity<MailInfoConstract.View, MailInfoConstract.Presenter> implements MailInfoConstract.View {


    @BindView(R.id.mLayoutArea)
    LinearLayout mLayoutArea;
    @BindView(R.id.mTxtArea)
    TextView mTxtArea;//区域
    @BindView(R.id.mEditAddrDetail)
    EditText mEditAddrDetail;
    @BindView(R.id.mEditUserName)
    EditText mEditUserName;
    @BindView(R.id.mEditPhone)
    EditText mEditPhone;
    @BindView(R.id.mEditNum)
    EditText mEditNum;
    @BindView(R.id.mTxtWeight)
    TextView mTxtWeight;
    //上一页传递过来的数据
    private int mGroupId;

    @Override
    protected int initContentView() {
        return R.layout.activity_mail_info;
    }

    @Override
    protected void initData() {
        setHeaderTitle(getString(R.string.mail_info_title));
        setRightImgResId(R.drawable.icon_scancode, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MailInfoActivity.this, WeChatCaptureActivity.class);
                startActivity(intent);

            }
        });

    }

    @Override
    protected void initProcess(Bundle savedInstanceState) {
        mGroupId = this.getIntent().getIntExtra("groupId", 3);


    }

    @OnClick({R.id.mBtnMainInfo, R.id.mTxtWeight, R.id.mLayoutArea})

    public void initListener(View v) {
        switch (v.getId()) {
            case R.id.mBtnMainInfo:
                if (checkForm()) {
                    MailInfoCommitBean mMailInfoCommitBean = new MailInfoCommitBean();
                    mMailInfoCommitBean.setmArea(mTxtArea.getText().toString());
                    mMailInfoCommitBean.setmAddrDetail(mEditAddrDetail.getText().toString());
                    mMailInfoCommitBean.setmUserName(mEditUserName.getText().toString());
                    mMailInfoCommitBean.setmPhone(mEditPhone.getText().toString());
                    mMailInfoCommitBean.setmMailNum(Integer.parseInt(mEditNum.getText().toString()));
                    mMailInfoCommitBean.setmMailWeight(Float.parseFloat(mTxtWeight.getText().toString()));
                    mMailInfoCommitBean.setGroupId(mGroupId);
                    getPresenter().httpCommit(mMailInfoCommitBean);
                }
                break;
            case R.id.mTxtWeight:
                selectSingle();
                break;
            case R.id.mLayoutArea:
                selectAddress();
                break;
            default:
                break;

        }

    }

    @NonNull
    @Override
    public MailInfoConstract.Presenter createPresenter() {
        return new MailInfoPresenter();
    }

    /**
     * 表单检测
     *
     * @author guofanghua
     */

    private boolean checkForm() {
        if (TextUtils.isEmpty(mTxtArea.getText())) {
            ToastUtils.show(R.string.mail_info_dq_warn);
            return false;
        }
        if (TextUtils.isEmpty(mEditAddrDetail.getText())) {
            ToastUtils.show(R.string.mail_info_addr_warn);
            return false;
        }
        if (TextUtils.isEmpty(mEditUserName.getText())) {
            ToastUtils.show(R.string.mail_info_mail_name_hint);
            return false;
        }
        if (TextUtils.isEmpty(mEditPhone.getText())) {
            ToastUtils.show(R.string.mail_info_mail_phone_hint);
            return false;
        }
        if (TextUtils.isEmpty(mEditNum.getText())) {
            ToastUtils.show(R.string.mail_info_mail_num_edit_hint);
            return false;
        }
        if (TextUtils.isEmpty(mTxtWeight.getText())) {
            ToastUtils.show(R.string.mail_info_mail_weight_hint);
            return false;
        }
        return true;

    }

    @Override
    public void commitSuccess() {
        ToastUtils.show(R.string.comm_commit_succss);
        Intent mIntent = new Intent(MailInfoActivity.this, ShareDialogActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putInt("groupId", mGroupId);
        mBundle.putString("groupName", getIntent().getStringExtra("groupName"));
        mBundle.putDouble("weightMax", getIntent().getDoubleExtra("weightMax", 0));
        mBundle.putDouble("weightMin", getIntent().getDoubleExtra("weightMin", 0));
        changeActivity(mIntent);
    }

    @Override
    public void commitFail(String errCode, String errMsg) {
        ToastUtils.show(errMsg);

    }

    /*选择重量*/
    private void selectSingle() {
        SinglePicker<String> picker = new SinglePicker<>(this,
                new String[]{
                        "1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "4"
                });
        picker.setCanLoop(false);
        picker.setTopBackgroundColor(0xFFEEEEEE);
        picker.setTopHeight(50);
        picker.setTopLineColor(0xFF33B5E5);
        picker.setTopLineHeight(1);
        picker.setTitleText("请选择(单位:kg)");
        picker.setTitleTextColor(getResources().getColor(R.color.color_5));
        picker.setTitleTextSize(16);
        picker.setCancelTextColor(getResources().getColor(R.color.color_1));
        picker.setCancelTextSize(18);
        picker.setSubmitTextColor(getResources().getColor(R.color.color_1));
        picker.setSubmitTextSize(18);
        picker.setSelectedTextColor(getResources().getColor(R.color.color_1));
        picker.setUnSelectedTextColor(0xFF999999);
        LineConfig config = new LineConfig();
        config.setColor(getResources().getColor(R.color.color_1));//线颜色
        config.setAlpha(140);//线透明度
        config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setItemWidth(200);
        picker.setBackgroundColor(0xFFE1E1E1);
        picker.setSelectedIndex(0);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                mTxtWeight.setText(item);
            }
        });
        picker.show();

    }

    /**
     * 选择省市区
     */
    private void selectAddress() {
        AddressPickTask task = new AddressPickTask(this);
        task.setHideProvince(false);
        task.setHideCounty(false);

        task.setCallback(new AddressPickTask.Callback() {
            @Override
            public void onAddressInitFailed() {
                ToastUtils.show("数据初始化失败");
            }

            @Override
            public void onAddressPicked(Province province, City city, County county) {
                mTxtArea.setText(province.getAreaName() + city.getAreaName() + county.getAreaName());
            }
        });
        task.execute("广东", "深圳", "南山");
    }
}
